﻿package kliment.utils {
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import kliment.math.Calc;
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.1.6
	 * 
	 * class name: kliment.utils.Align
	 */
	public class Align {
		static private const _X:String = 'x';
		static private const _Y:String = 'y';
		static private const _WIDTH:String = 'width';
		static private const _HEIGHT:String = 'height';
		static public var round:Boolean = true;
		static public var roundFunction:Function = Math.round;
	
		static public function left(target:Object, space:Object):Number {
			return _setProperty(target, _X, _getValue(space, _X));
		}
		
		static public function right(target:Object, space:Object):Number {
			return _setProperty(target, _X, _getValue(space, _X) + _getValue(space, _WIDTH) - _getValue(target, _WIDTH)); 
		}
		
		static public function top(target:Object, space:Object):Number {
			return _setProperty(target, _Y, _getValue(space, _Y)); 
		}
		
		static public function bottom(target:Object, space:Object):Number {
			return _setProperty(target, _Y, _getValue(space, _Y) + _getValue(space, _HEIGHT) - _getValue(target, _HEIGHT)); 
		}
		
		static public function topLeft(target:Object, space:Object):Point {
			return new Point(left(target, space), top(target, space));
		}
		
		static public function topRight(target:Object, space:Object):Point {
			return new Point(right(target, space), top(target, space));
		}
		
		static public function bottomRight(target:Object, space:Object):Point {
			return new Point(right(target, space), bottom(target, space));
		}
		
		static public function bottomLeft(target:Object, space:Object):Point {
			return new Point(left(target, space), bottom(target, space));
		}
		
		static public function center(target:Object, space:Object):Point {
			return new Point(centerHorizontal(target, space), centerVertical(target, space));
		}
		
		static public function centerTop(target:Object, space:Object):Point {
			return new Point(centerHorizontal(target, space), top(target, space));
		}
		
		static public function centerRight(target:Object, space:Object):Point {
			return new Point(centerVertical(target, space), right(target, space));
		}
		
		static public function centerBottom(target:Object, space:Object):Point {
			return new Point(centerHorizontal(target, space), bottom(target, space));
		}
		
		static public function centerLeft(target:Object, space:Object):Point {
			return new Point(centerVertical(target, space), left(target, space));
		}
		
		static public function centerHorizontal(target:Object, space:Object):Number {
			return _setProperty(target, _X, _getValue(space, _X) + (Calc.half(_getValue(space, _WIDTH)) - Calc.half(_getValue(target, _WIDTH)))); 
		}
		
		static public function centerVertical(target:Object, space:Object):Number {
			return _setProperty(target, _Y, _getValue(space, _Y) + (Calc.half(_getValue(space, _HEIGHT)) - Calc.half(_getValue(target, _HEIGHT)))); 
		}
		
		static public function matchWidth(target:Object, space:Object):Number {
			return _setProperty(target, _WIDTH, _getValue(space, _WIDTH));
		}
		
		static public function matchHeight(target:Object, space:Object):Number {
			return _setProperty(target, _HEIGHT, _getValue(space, _HEIGHT));
		}
		
		static public function moreTop(target:Object, space:Object, interval:Number = 0):Number {
			return _setProperty(target, _Y, _getValue(space, _Y) - _getValue(target, _HEIGHT) - interval);
		}
		
		static public function moreRight(target:Object, space:Object, interval:Number = 0):Number {
			return _setProperty(target, _X, _getValue(space, _X) + _getValue(space, _WIDTH) + interval);
		}
		
		static public function moreBottom(target:Object, space:Object, interval:Number = 0):Number {
			return _setProperty(target, _Y, _getValue(space, _Y) + _getValue(space, _HEIGHT) +interval);
		}
		
		static public function moreLeft(target:Object, space:Object, interval:Number = 0):Number {
			return _setProperty(target, _X, _getValue(space, _X) - _getValue(target, _WIDTH) - interval);
		}
		
		static public function horisontalLine(elements:Array, interval:Number = 0):Number {
			var nextX:Number = 0;
			var item:DisplayObject;
			var lenght:uint = elements.length;
			for (var i:uint = 0; i < lenght; ++i ) {
				item = elements[i];
				if (item.visible) {
					item.x = nextX;
					nextX += item.width + interval;
				} else {
					item.x = 0;
				}
			}
			return nextX - interval;
		}
		
		static public function centerMoreTop(target:Object, space:Object):Point {
			return new Point(centerHorizontal(target, space), moreTop(target, space));
		}
		
		static public function centerMoreRight(target:Object, space:Object):Point {
			return new Point(moreRight(target, space), centerVertical(target, space));
		}
		
		static public function centerMoreBottom(target:Object, space:Object):Point {
			return new Point(centerHorizontal(target, space), moreBottom(target, space));
		}
		
		static public function centerMoreLeft(target:Object, space:Object):Point {
			return new Point(moreLeft(target, space), centerVertical(target, space));
		}
		
		static private function _setProperty(target:Object, property:String, value:Number):Number {
			if (round)
				value = roundFunction(value);
			return Number(_Object.setProperty(target, property, value));
		}
		
		static private function _getValue(target:Object, property:String):Number {
			return Number(target[property]);
		}
		
	}

}